﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace NExplorer
{
	internal class HistoryList
	{
		private readonly List<DirectoryInfo>	_history = new List<DirectoryInfo>();
		private int								_currentIndex = 0;
		private const int						c_max = 100;

		public int CurrentIndex
		{
			get { return _currentIndex; }
		}

		public DirectoryInfo MoveBack()
		{
			DirectoryInfo info;

			if (_currentIndex > 0)
			{
				info = _history[--_currentIndex];
			}
			else
			{
				throw new InvalidOperationException();
			}

			return info;
		}

		public DirectoryInfo MoveForward()
		{
			DirectoryInfo info;

			if (_currentIndex < _history.Count - 1)
			{
				info = _history[++_currentIndex];
			}
			else
			{
				throw new InvalidOperationException();
			}

			return info;
		}

		public void Add(DirectoryInfo DirectoryInfo)
		{
			if (_currentIndex < _history.Count - 1)
			{
				_history.RemoveRange(_currentIndex + 1, _history.Count - _currentIndex - 1);
			}

			if (_history.Count == c_max)
			{
				_history.RemoveAt(0);
			}

			_history.Add(DirectoryInfo);
			_currentIndex = _history.Count - 1;
		}

		public bool CanMoveForward
		{
			get { return _currentIndex < _history.Count - 1;  }
		}

		public bool CanMoveBack
		{
			get { return _currentIndex > 0; }
		}

		public string GetNextText()
		{
			string text = null;

			if (CanMoveForward)
			{
				text = "Forward to " + GetName(_currentIndex + 1);
			}

			return text;
		}

		private string GetName(int index)
		{
		    return _history[index].Name;
		}

		public string GetPreviousText()
		{
			string text = null;

			if (CanMoveBack)
			{
				text = "Back to " + GetName(_currentIndex - 1);
			}

			return text;
		}
	}
}
